package com.semidata.trp.business.config;

import java.io.File;

import com.semidata.rtd.core.config.DefaultValue;
import com.semidata.rtd.core.config.DefaultValueClass;
import com.semidata.rtd.core.config.DefaultValueHandler;
import com.semidata.rtd.core.config.Handler;
import com.semidata.rtd.core.config.Property;
import com.semidata.rtd.core.config.UrlHandler;

public class Config extends com.semidata.rtd.core.config.Config {

    private static String DEFAULT_CONFIG_FILE = "conf/business.conf";
    private static Config config = null;

    public static void init(String configFile) {
        if (configFile == null)
            configFile = Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath();

        config = new Config(configFile);
    }

    public static class DefaultLogDir implements DefaultValueHandler {

        @Override
        public String value() {
            File file = new File(Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath());
            file = new File(file.getParentFile().getParentFile(), "logs/business");
            return file.getAbsolutePath();
        }

    }

    public static class DefaultDataDir implements DefaultValueHandler {

        @Override
        public String value() {
            File file = new File(Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath());
            file = new File(file.getParentFile().getParentFile(), "data/business");
            return file.getAbsolutePath();
        }

    }

    public static class DefaultDebugDataDir implements DefaultValueHandler {

        @Override
        public String value() {
            File file = new File(Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath());
            file = new File(file.getParentFile().getParentFile(), "data/business/debug");
            return file.getAbsolutePath();
        }

    }

    public static Config getConfig() {
        return config;
    }

    private Config(String configFile) {
        initConfig(configFile);
    }

    @Property("http.port")
    @DefaultValue("7000")
    private int httpPort;

    @Property(value = "http.threads")
    @DefaultValue("16")
    private int httpThreads;

    @Property(value = "log.root")
    @DefaultValueClass(DefaultLogDir.class)
    private String logRoot;

    @Property("log.debug.enabled")
    @DefaultValue("false")
    private boolean logDebugEnabled;

    @Property("url.report.monitor")
    @DefaultValue("/report/send/")
    @Handler(UrlHandler.class)
    private String reportMonitorUrl;

    @Property("url.report.provider")
    @DefaultValue("/report/get/")
    @Handler(UrlHandler.class)
    private String reportProviderUrl;

    @Property("url.register")
    @DefaultValue("/register/")
    @Handler(UrlHandler.class)
    private String registerUrl;

    @Property("report.data.dir")
    @DefaultValueClass(DefaultDataDir.class)
    private String reportDataDir;

    @Property("debug.data.dir")
    @DefaultValueClass(DefaultDebugDataDir.class)
    private String debugDataDir;

    public int getHttpPort() {
        return httpPort;
    }

    public int getHttpThreads() {
        return httpThreads;
    }

    public String getLogRoot() {
        return logRoot;
    }

    public boolean isLogDebugEnabled() {
        return logDebugEnabled;
    }

    public String getReportMonitorUrl() {
        return reportMonitorUrl;
    }

    public String getReportProviderUrl() {
        return reportProviderUrl;
    }

    public String getRegisterUrl() {
        return registerUrl;
    }

    public String getReportDataDir() {
        return reportDataDir;
    }

    public String getDebugDataDir() {
        return debugDataDir;
    }

    @Override
    public String toString() {
        return "Config [httpPort=" + httpPort + ", httpThreads=" + httpThreads + ", logRoot=" + logRoot + ", logDebugEnabled=" + logDebugEnabled + ", reportMonitorUrl=" + reportMonitorUrl
                + ", reportProviderUrl=" + reportProviderUrl + ", registerUrl=" + registerUrl + ", reportDataDir=" + reportDataDir + "]";
    }

    public static void main(String[] args) {
        init(null);
        System.out.println(getConfig().toString());
    }

}
